// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import Client from '@api/face/api/FaceNewPersonApi'

import ComClass from '@api/ComQuery'
import ListClass from '@api/face/model/FaceNewPersonReq'
import DelClass from '@api/face/model/FaceNewPersonDeleteReq'
import SubmitClass from '@api/face/model/SyncOnePersonManyDeviceReq'
import BaseClass from '@api/face/model/FaceNewPersonDto'

const AddClass = ListClass
const EditClass = ListClass

const CLIENT = CORE.grpcCLIENT(Client)
const COM = [ComClass, 'comQuery']
const BASE = [BaseClass, 'dto']

// ============================== 导出远程接口 ============================== //

export default {
  // —————————————————————————————— 列表 —————————————————————————————— //

  async getList(param, TIP = '查询人员列表') {
    const DTO = CORE.grpcDTO(ListClass, param, COM, BASE)
    const resp = await CORE.grpc(TIP, CLIENT, 'faceNewPersonQuery', DTO)
    return [resp.comQuery.totalCount, resp.content || []]
  },
  transList(arr, { state: { tag: { select } } }) {
    arr.forEach((item) => {
      const successDevice = item.faceModelRecordDto.filter((item) => item.syncStatus === 3)
      item.syncCount = `${successDevice.length} / ${item.faceModelRecordDto.length}`
      item.tagList = select.filter((tag) => item.labelIds.includes(tag.id))
    })
    return arr
  },

  // —————————————————————————————— 增 —————————————————————————————— //

  async add(one, TIP = '新增人员') {
    const DTO = CORE.grpcDTO(AddClass, one, BASE)
    await CORE.grpc(TIP, CLIENT, 'faceNewPersonSave', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 改 —————————————————————————————— //

  async edit(one, TIP = '修改人员') {
    const DTO = CORE.grpcDTO(EditClass, one, BASE)
    await CORE.grpc(TIP, CLIENT, 'faceNewPersonUpdate', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 删 —————————————————————————————— //

  async del({ id }, TIP = '删除人员') {
    const DTO = CORE.grpcDTO(DelClass, { ids: id })
    await CORE.grpc(TIP, CLIENT, 'faceNewPersonDelete', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 提交 —————————————————————————————— //

  transSubmit(one) {
    return { id: one.facePersonId, deviceIds: [one.faceDeviceId] }
  },
  async submit(one, TIP = '重新建模') {
    const DTO = CORE.grpcDTO(SubmitClass, one)
    await CORE.grpc(TIP, CLIENT, 'syncOnePersonManyDevice', DTO)
    return `${TIP}：已提交`
  },
}
